package org.hot100_6.leetcode141;

import org.algorithm.List.ListNode;
import org.utils.ListNode.ListNodeUtils;

public class Solution {

    public static void main(String[] args) {
        int [] test = {3,2,0,-4};
        ListNode listNode = ListNodeUtils.createListNode(test);
        boolean b = hasCycle(listNode);
        System.out.println(b);
    }

    public static boolean hasCycle(ListNode head) {

        if (head == null || head.next == null){
            return false;
        }

        ListNode slow = head;
        ListNode fast = head.next;

        while (slow!=fast){
            if (fast==null || fast.next==null){
                return false;
            }
            // 慢指针移动一步，快指针移动两步。如果都是一步，那么快慢指针在有环的情况下不会相遇，因为二者速度相同
            slow = slow.next;
            fast = fast.next.next;
        }

        return true;
    }
}
